<?php 
/**
 * 公共控制器
 */
class CommonController extends Controller{
	/**
	 * 分配顶级分类
	 */
	public function topCate(){
		//问题库处理(顶级分类)
		$topCate = M()->query("SELECT * FROM hd_category WHERE pid=0");
		$this->assign('topCate',$topCate);
	}
	public function todayCate(){
		    //本日回答问题最多的人
			$zero =  strtotime(date('Y-m-d'));
			
            $sql = "select u.*,count(*) as c from hd_answer as a inner join hd_user as u on a.uid=u.uid where time >{$zero} group by uid order by c desc limit 1";
			$noToday = M()->query($sql);
			
			
			$this->assign('noToday',$noToday);
	}
	
	public function solveCate(){
			//处理待解决的问题
			$noSolve = M()->query("SELECT * FROM hd_ask WHERE solve=0");
			$this->assign('noSolve',$noSolve);
	}
	public function rewardCate(){
		    //高分悬赏的问题
			$noReward = M()->query("SELECT * FROM hd_ask WHERE reward>1 and answer=0 order by reward desc");
			$this->assign('noReward',$noReward);
	}
	public function acceptCate(){
			//后盾问答助人光荣榜
			$noAccept = M()->query("SELECT * FROM hd_user order by accept desc");
			$this->assign('noAccept',$noAccept);
	}
	public function answerCate(){
			//历史回答问题最多的人
			$noAnswer = M()->query("SELECT * FROM hd_user order by answer desc limit 1");
			$answer=$noAnswer[0]['answer'];
			$accept=$noAnswer[0]['accept'];
			if($answer!=0){
				$take_n=round($accept/$answer*100,2).'%';
				
				$this->assign('take_n',$take_n);
			}else{
				$take_n=0;
				$this->assign('take_n',$take_n);
			}
			$this->assign('noAnswer',$noAnswer);
	}
	
	public function wenCate(){
		    //累计提问数量
		    $wenAnswer = M()->query("SELECT count(*) as num FROM hd_ask");
		    
		    $this->assign('wenAnswer',$wenAnswer);
   }
	
	public function gerenCate(){
		    //获得session uid
		   $uid=$_SESSION['uid'];
		   //个人中心
		   $oneCate=M()->query("SELECT * FROM hd_user WHERE uid={$uid}");
	       $answer=$oneCate[0]['answer'];
		   $accept=$oneCate[0]['accept'];
			if($answer!=0){
				$take_l=round($accept/$answer*100,2).'%';
				
				$this->assign('take_l',$take_l);
			}else{
				$take_l=0;
				$this->assign('take_l',$take_l);
			}
		   $this->assign("oneCate",$oneCate);
		   
		   //关联问题表和用户表 只取两条
		   $aaCate=M()->query("SELECT * FROM hd_ask as a join hd_user as u on a.uid=u.uid WHERE u.uid={$uid} ORDER BY TIME DESC LIMIT 2");
		   foreach($aaCate as $k=>$v){
		   		$aaCate[$k]['time'] = date('Y-m-d h:i:s',$aaCate[$k]['time']);
		   }
		   $this->assign("aaCate",$aaCate);
		   //统计用户问题数量
		   $numCate=M()->query("SELECT count(*) as n FROM hd_ask WHERE uid={$uid}");
		   $this->assign("numCate",$numCate);
		   
		   //关联回答表和用户表 只取两条
		   $anDate=M()->query("SELECT * FROM hd_answer as a join hd_user as u on a.uid=u.uid WHERE u.uid={$uid} ORDER BY TIME DESC LIMIT 2");
		   foreach($anDate as $k=>$v){
		   		$anDate[$k]['time'] = date('Y-m-d h:i:s',$anDate[$k]['time']);
		   }
		   
		   $this->assign("anDate",$anDate);
		  
		   //统计用户回答数量
		   $numDate=M()->query("SELECT count(*) as nu FROM hd_answer WHERE uid={$uid}");
		   $this->assign("numDate",$numDate);
		   
     }
     
	 
	 
	 public function face(){
		    $uid=$_SESSION['uid'];
			   
		   if(IS_POST){
				$upload = new Upload();
				$path = $upload->up();
				
				$sql = "UPDATE hd_user SET face='{$path[0]}' WHERE uid={$uid}";
				M()->exec($sql);
				$this->error('上传成功');
			}
			
			$userInfo = M()->query("SELECT * FROM hd_user WHERE uid={$uid}");
			
			$this->assign('userInfo',$userInfo);
     }

	
}





